import matplotlib.pyplot as plt
import pandas as pd
from wordcloud import WordCloud

# 读取CSV文件并创建数据框
df = pd.read_csv('bilibili_videos.csv', encoding='utf-8')

# 将标题、视频简介和推荐理由合并为一个文本
# 处理可能的空值（NaN）
df['标题'] = df['标题'].fillna('')
df['视频描述'] = df['视频描述'].fillna('')

# 然后连接字符串
text = ' '.join(df['标题'].astype(str)) + ' ' + ' '.join(df['视频描述'].astype(str))

# 创建词云对象
wordcloud = WordCloud(width=800, height=400, background_color='white', font_path='C:/Windows/Fonts/simsun.ttc').generate(text)


# 绘制词云图
plt.figure(figsize=(10, 6))
plt.imshow(wordcloud, interpolation='bilinear')
plt.axis('off')
plt.show()
